import 'dart:io';

import 'package:dio/dio.dart';
import '../config/http_config.dart';

class HttpRequest {
  static final BaseOptions options = BaseOptions(
    baseUrl: HttpConfig.baseURL,
    connectTimeout: HttpConfig.timeout
  );


 

  static final Dio dio = Dio(options);


  static Future<T> request<T>(
    String url, 
    {
      String method = 'get',
      params,
      inter 
    }) async {
  String cookie = 'bid=OMN8pFDfo4A; _pk_ses.100001.4cf6=*; __utma=30149280.1169598448.1638674116.1638674116.1638674116.1; __utmb=30149280.0.10.1638674116; __utmc=30149280; __utmz=30149280.1638674116.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=223695111.1742457439.1638674116.1638674116.1638674116.1; __utmb=223695111.0.10.1638674116; __utmc=223695111; __utmz=223695111.1638674116.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); push_noty_num=0; push_doumail_num=0; dbcl2="251211263:zs07TZR2FNs"; ck=iMbf; _pk_id.100001.4cf6=252abde993cc54c7.1638674116.1.1638674247.1638674116.';

     Map<String, dynamic> headers = new Map();
  headers['Cookie'] = cookie;

    // 1. 请求单独配置
    final options = Options(
      method: method,
      headers: headers
    );

    // 2.添加拦截器
    Interceptor dInter = InterceptorsWrapper(
      onRequest: (RequestOptions options, RequestInterceptorHandler handler) {

        // print('拦截了请求:$options');

        return handler.next(options);
      },
      onResponse: (Response response, ResponseInterceptorHandler handler) {
        // print('拦截了响应:$response');
        return handler.next(response);
      },
      onError: (DioError error, ErrorInterceptorHandler handler) {
        // print('拦截了错误:$error');
        return handler.next(error);
      }
    );

    dio.interceptors.add(dInter);

    print('$url $params');

    // 3. 发送网络请求
    try{
      Response response = await dio.request<T>(url, queryParameters: params, options: options);
      return response.data;
    }on DioError catch(e) {
      return Future.error(e);
    }
  }
}